﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyWeb.Data;
using MyWeb.Business;
using MyWeb.Common;
using System.Security.Cryptography;

namespace MyWeb.Admins
{
    public partial class Users : System.Web.UI.Page
    {
        static string Id = "";
        static bool Insert = false;
        static string Level = "";
        static string Lang = "";
        static string pass = "";
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               if (Session["Admin"].Equals("1"))
                {
                    lbtDeleteT.Attributes.Add("onClick", "javascript:return confirm('Bạn có muốn xóa?');");
                    lbtDeleteB.Attributes.Add("onClick", "javascript:return confirm('Bạn có muốn xóa?');");
                    NumberClass.OnlyInputNumber(txtOrd);
                    Lang = Session["Lang"].ToString();
                    loadddlDep();
                    loadddlpa();
                    loadddlPos();
                    BindGrid();
                }
               else
               {
                   pnUpdate.Visible = false;
                  pnView.Visible = false;
               }
            }
        }

        private void BindGrid()
        {
            grdUsers.DataSource = UsersService.Users_GetByAll();
            grdUsers.DataBind();
            if (grdUsers.PageCount <= 1)
            {
                grdUsers.PagerStyle.Visible = false;
            }
        }
        private void loadddlDep()
        {
            List<Data.Departments> listde = new List<Data.Departments>();
            listde = DepartmentsService.Departments_GetByAll();
            foreach (Data.Departments dpm in listde)
            {
                ddlDepartment.Items.Add(new ListItem(dpm.Name,dpm.dpID));
            }
        }

        private void loadddlpa()
        {
            List<Data.Parts> listpa = new List<Data.Parts>();
            listpa = PartsService.Parts_GetByAll();
            foreach (Data.Parts pa in listpa)
            {
                ddlPart.Items.Add(new ListItem(pa.Name, pa.paID));
            }
        }
        private void loadddlPos()
        {
            List<Data.Positions> listpo = new List<Data.Positions>();
            listpo = PositionsService.Positions_GetByAll();
            foreach (Data.Positions po in listpo)
            {
                
                ddlPosition.Items.Add(new ListItem(po.Name, po.psID));
            }
        }
        protected void grdUsers_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            ListItemType itemType = e.Item.ItemType;
            if ((itemType != ListItemType.Footer) && (itemType != ListItemType.Separator))
            {
                if (itemType == ListItemType.Header)
                {
                    object checkBox = e.Item.FindControl("chkSelectAll");
                    if ((checkBox != null))
                    {
                        ((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelectAll_OnClick(this)");
                    }
                }
                else
                {
                    string tableRowId = grdUsers.ClientID + "_row" + e.Item.ItemIndex.ToString();
                    e.Item.Attributes.Add("id", tableRowId);
                    object checkBox = e.Item.FindControl("chkSelect");
                    if ((checkBox != null))
                    {
                        e.Item.Attributes.Add("onMouseMove", "Javascript:chkSelect_OnMouseMove(this)");
                        e.Item.Attributes.Add("onMouseOut", "Javascript:chkSelect_OnMouseOut(this," + e.Item.ItemIndex.ToString() + ")");
                        ((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelect_OnClick(this," + e.Item.ItemIndex.ToString() + ")");
                    }
                    Label lblname = e.Item.FindControl("lblname") as Label;
                    lblname.Text = StringClass.ShowNameLevel(((Data.Users)e.Item.DataItem).Name, ((Data.Users)e.Item.DataItem).Level);
                }
            }
        }

        protected void grdUsers_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            grdUsers.CurrentPageIndex = e.NewPageIndex;
            BindGrid();
        }

        protected void grdUsers_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            string strCA = e.CommandArgument.ToString();
            switch (e.CommandName)
            {
                case "AddSub":
                    Level = strCA;
                    AddButton_Click(source, e);
                    break;
                case "ConTract":
                    Response.Redirect("/Admin/Contract.aspx?ID=" + strCA);
                    break;
                case "work":
                    Response.Redirect("/Admin/Work.aspx?ID=" + strCA);
                    break;
                case "Edit":
                    Insert = false;
                    Id = strCA;
                    List<Data.Users> listE = UsersService.Users_GetById(Id);
                    Level = listE[0].Level.Substring(0, listE[0].Level.Length - 5);
                    ddlDepartment.SelectedValue= listE[0].dpID;
                    ddlPosition.SelectedValue = listE[0].psID;
                    ddlPart.SelectedValue = listE[0].paId;
                    txtCode.Text = listE[0].Code;
                    txtUser.Text = listE[0].User;
                    txtPass.Text = "Pass đã được mã hoá";
                    pass = listE[0].Pass;
                    txtName.Text = listE[0].Name;
                    txtDate.Text = DateTimeClass.ConvertDateTime(listE[0].Date);
                    txtAdd.Text = listE[0].Add;
                    txtTel.Text = listE[0].Tel;
                    txtOrd.Text = listE[0].Ord;
                    txtEmail.Text = listE[0].Email;
                    txtAdmin.Text = listE[0].Admin;
                    txtImg.Text = listE[0].Img;
                    imgImg.ImageUrl = listE[0].Img.Length > 0 ? listE[0].Img : "";
                    txtCN.Text = listE[0].CN;
                    txtcanAccept.Text = listE[0].canAccept;
                    pnView.Visible = false;
                    pnUpdate.Visible = true;
                    break;
                case "Delete":
                    UsersService.Users_Delete(strCA);
                    BindGrid();
                    break;
                
            }
        }

        protected void AddButton_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = true;
            ControlClass.ResetControlValues(this);
            txtDate.Text = DateTimeClass.ConvertDateTime(DateTime.Now, "dd/MM/yyyy hh:mm:ss tt");
            pnView.Visible = false;
            Insert = true;
        }

        protected void DeleteButton_Click(object sender, EventArgs e)
        {
            DataGridItem item = default(DataGridItem);
            for (int i = 0; i < grdUsers.Items.Count; i++)
            {
                item = grdUsers.Items[i];
                if (item.ItemType == ListItemType.AlternatingItem | item.ItemType == ListItemType.Item)
                {
                    if (((CheckBox)item.FindControl("ChkSelect")).Checked)
                    {
                        string strId = item.Cells[1].Text;
                        UsersService.Users_Delete(strId);
                    }
                }
            }
            grdUsers.CurrentPageIndex = 0;
            BindGrid();
        }

        protected void RefreshButton_Click(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void Update_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                MD5 md5=MD5.Create();
                Data.Users obj = new Data.Users();
                obj.uID = Id;
                obj.dpID = ddlDepartment.SelectedValue;
                obj.psID = ddlPosition.SelectedValue;
                obj.paId = ddlPart.SelectedValue;
                obj.Code = txtCode.Text;
                obj.User = txtUser.Text;
                if (!txtPass.Text.Trim().Equals("Pass đã được mã hoá"))
                    obj.Pass = MaHoaPass.GetMd5Hash(md5, txtPass.Text.Trim());
                else obj.Pass = pass;
                obj.Name = txtName.Text;
                obj.Date = DateTimeClass.ConvertDateTime(txtDate.Text, "MM/dd/yyyy HH:mm:ss");
                obj.Add = txtAdd.Text;
                obj.Tel = txtTel.Text;
                obj.Level = Level + "00000";
                obj.Ord = txtOrd.Text != "" ? txtOrd.Text : "1";
                obj.Email = txtEmail.Text;
                obj.Admin = txtAdmin.Text;
                obj.Img = txtImg.Text;
                obj.CN = txtCN.Text;
                
                obj.canAccept = txtcanAccept.Text;
                if (Insert == true)
                {
                    UsersService.Users_Insert(obj);
                }
                else
                {
                    UsersService.Users_Update(obj);
                }
                BindGrid();
                pnView.Visible = true;
                pnUpdate.Visible = false;
                Level = "";
                Insert = false;
            }
        }

        protected void Back_Click(object sender, EventArgs e)
        {
            pnView.Visible = true;
            pnUpdate.Visible = false;
            Level = "";
            Insert = false;
        }
    }
}